local tunquan = fk.CreateSkill{
  name = "plum__tunquan",
}

Fk:loadTranslationTable{
  ["plum__tunquan"] = "吞权",
  [":plum__tunquan"] = "结束阶段，你可以弃置任意张牌，然后视为使用一张目标为手牌数不小于你的角色的【万箭齐发】。期间你手牌数由最小变为最大后，你加1点体力上限并回复1点体力。",

  ["#plum__tunquan-discard"] = "吞权：可以弃置任意张牌，视为使用目标为手牌数不小于你的角色的【万箭齐发】",

  ["$plum__tunquan1"] = "",
  ["$plum__tunquan2"] = "",
  ["$plum__tunquan3"] = "",
}

tunquan:addEffect(fk.EventPhaseStart, {
  mute = true,
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill("plum__tunquan") and player == target
    and data.phase == Player.Finish and not player:isNude()
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askToDiscard(player, {
      prompt = "#plum__tunquan-discard",
      skill_name = tunquan.name,
      include_equip = true,
      min_num = 1,
      max_num = 999,
      cancelable = true,
      skip = true,
    })
    if #cards > 0 then
      event:setCostData(self, cards)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local cards = event:getCostData(self) ---@type integer[]
    local room = player.room
    room:notifySkillInvoked(player, tunquan.name)
    player:broadcastSkillInvoke(tunquan.name, math.random(1,2))
    room:throwCard(cards, tunquan.name, player, player)

    if player:isAlive() then
      local card = Fk:cloneCard("archery_attack")
      card.skillName = tunquan.name
      local tos = table.filter(card:getAvailableTargets(player), function (p)
        return p:getHandcardNum() >= player:getHandcardNum()
      end)
      if #tos > 0 then
        if table.every(player.room:getAlivePlayers(false), function (p)
          return player:getHandcardNum() <= p:getHandcardNum()
        end) then
          room:setPlayerMark(player, tunquan.name, true)
        end
        room:useCard{
          from = player,
          tos = tos,
          card = card
        }
        room:setPlayerMark(player, tunquan.name, 0)
      end
    end
  end
})

tunquan:addEffect(fk.AfterCardsMove, {
  mute = true,
  anim_type = "support",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(tunquan.name) then
      local mark = player:getMark(tunquan.name) ---@type integer|true
      if mark ~= 0 and table.every(player.room:getAlivePlayers(false), function (p)
        return player:getHandcardNum() >= p:getHandcardNum()
      end) then
        player.room:setPlayerMark(player, tunquan.name, 0)
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, tunquan.name)
    player:broadcastSkillInvoke(tunquan.name, 3)

    if player:isAlive() then
      room:changeMaxHp(player, 1)
    end
    if player:isWounded() then
      room:recover{
        who = player,
        num = 1,
        skillName = tunquan.name,
        recoverBy = player,
      }
    end
  end
})

return tunquan